const router = require('koa-router')();
router.prefix(appConfig.authPrefixUrl+'/desktop/carnum_datas');
const CarnumDatasService = appRequire('service/desktop/CarnumDatasService.js');
const CarnumDatasDeptTypeService = appRequire('service/desktop/CarnumDatasDeptTypeService.js');
const ResultVo = appRequire('vo/ResultVo.js');

router.get('/index',async(ctx)=>{
  ctx.body="carnum_datas 你好";
});

router.post('/save',async(ctx)=>{
  try{
    let carnumDatas = ctx.request.body;
    console.log(carnumDatas);
    await CarnumDatasService.save(carnumDatas);
    ctx.body = ResultVo.success('success');
  }catch(err){
    //集中处理错误
    err.msg = "新增车辆号码数据失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/deleteOne',async(ctx)=>{
  try{
    let carnumDatas = ctx.query;
    carnumDatas = await CarnumDatasService.deleteOne(carnumDatas.id);
    ctx.body = ResultVo.success('success',carnumDatas);
  }catch(err){
    //集中处理错误
    err.msg = "删除车辆号码数据失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.post('/deleteMany',async(ctx)=>{
  try{
    let param = ctx.request.body;
    console.log(param);
    param = await CarnumDatasService.deleteMany(param.ids);
    ctx.body = ResultVo.success('success',param);
  }catch(err){
    //集中处理错误
    err.msg = "批量删除车辆号码数据失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/findOne',async(ctx)=>{
  try{
    let carnumDatas = ctx.query;
    carnumDatas = await CarnumDatasService.findOne(carnumDatas.id);
    let deptNames =await CarnumDatasDeptTypeService.findAll();
    carnumDatas.deptNames = deptNames;
    console.log(carnumDatas);
    ctx.body = ResultVo.success('success',carnumDatas);
  }catch(err){
    //集中处理错误
    err.msg = "查找车辆号码数据失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/findDeptNames',async(ctx)=>{
  try{
    let deptNames =await CarnumDatasDeptTypeService.findAll();
    ctx.body = ResultVo.success('success',deptNames);
  }catch(err){
    //集中处理错误
    err.msg = "查找车辆号码列表数据失败!";
    ctx.app.emit('error', err, ctx);
  }
});

router.get('/findAll',async(ctx)=>{
  try{
    let data = await CarnumDatasService.findAll();
    ctx.body = ResultVo.success('success',data);
  }catch(error){
    ctx.body = ResultVo.error(-1,'获取全部车辆号码数据数据失败!');
  }
});
router.get('/findByPage',async(ctx)=>{
  try{
    let page = ctx.query;
    console.log(page);
    let data = await CarnumDatasService.findByPage(page);
    ctx.body = data;
  }catch(error){
    ctx.body = ResultVo.error(-1,'获取分页车辆号码数据数据失败!');
  }
});
router.post('/publishData',async(ctx)=>{
  try{
    let param = ctx.request.body;
    console.log(param);
    param = await CarnumDatasService.publishData();
    ctx.body = ResultVo.success('success',param);
  }catch(err){
    //集中处理错误
    err.msg = "发布车辆号码数据失败!";
    ctx.app.emit('error', err, ctx);
  }
});


module.exports = router;